#include <Arduino.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
//----------使用说明-----------
//私钥：SeiP2ViJVtkngR1s5
//接口地址
//https://api.seniverse.com/   v3/weather/now.json?
                            // key=your_api_key
                            // &location=beijing
                            // &language=zh-Hans
                            // &unit=c


// WiFi settings
const char *ssid="LWT";
const char *password="Wjs717601";
const char* host = "api.seniverse.com";// API server

WiFiClient client;
int httpPort = 80;

String key="SeiP2ViJVtkngR1s5";
String location="luoyang";
String language="en";

String jsonAnswer;
int jsonIndex;



void setup()
{
  Serial.begin(115200);
  delay(10);
  //WIFI开始连接
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) //等待wifi连接成功
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}



void loop()
{
  if (!client.connect(host, httpPort)) //TCP连接是否成功，不成功再重试
  {
    Serial.println("connection failed");
    return;
  }
  // We now create a URI for the request
  String url = "/v3/weather/daily.json?key="+key+"&location="+location+"&language="+language+"&unit=c&start=0&days=3";
  //发送request请求
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                          "Host: " + host + "\r\n" +
                          "Connection: close\r\n\r\n");
  delay(100);

  //将每行的返回值加入到answer
  String answer;
  while(client.available())
  {
    String line = client.readStringUntil('\r');//terminator: 终止字符。用于设置终止函数执行的字符信息。设备在读取数据时一旦读取到此终止字符，将会结束函数执行。允许使用char类型。
    answer += line;
  }

  client.stop();//关闭连接
  Serial.println();
  Serial.println("closing connection");

  for (int i = 0; i < answer.length(); i++) //如果开头为{目录就为i
  {
    if (answer[i] == '{')
    {
      jsonIndex = i;
      break;
    }
  }

  // Get JSON data
  jsonAnswer = answer.substring(jsonIndex);
  Serial.println();
  Serial.println("JSON answer: ");
  Serial.println(jsonAnswer);

  DynamicJsonDocument doc(1536);
  DeserializationError error = deserializeJson(doc, jsonAnswer);
  JsonObject results_0 = doc["results"][0];

  JsonObject results_0_location = results_0["location"];
  const char* results_0_location_id = results_0_location["id"]; // "WQPUYY7S4GV2"
  const char* results_0_location_name = results_0_location["name"]; // "洛阳"
  const char* results_0_location_country = results_0_location["country"]; // "CN"
  const char* results_0_location_path = results_0_location["path"]; // "洛阳,洛阳,河南,中国"
  const char* results_0_location_timezone = results_0_location["timezone"]; // "Asia/Shanghai"
  const char* results_0_location_timezone_offset = results_0_location["timezone_offset"]; // "+08:00"

  for (JsonObject results_0_daily_item : results_0["daily"].as<JsonArray>()) {

    const char* results_0_daily_item_date = results_0_daily_item["date"]; // "2022-02-17", "2022-02-18", ...
    const char* results_0_daily_item_text_day = results_0_daily_item["text_day"]; // "小雪", "小雪", "多云"
    const char* results_0_daily_item_text_night = results_0_daily_item["text_night"]; // "阴", "阴", "多云"
    const char* results_0_daily_item_code_night = results_0_daily_item["code_night"]; // "9", "9", "4"
    const char* results_0_daily_item_high = results_0_daily_item["high"]; // "1", "3", "6"
    const char* results_0_daily_item_low = results_0_daily_item["low"]; // "-3", "-4", "-4"
    const char* results_0_daily_item_rainfall = results_0_daily_item["rainfall"]; // "1.80", "0.37", "0.00"
    const char* results_0_daily_item_precip = results_0_daily_item["precip"]; // "0.98", "0.64", "0.10"
    const char* results_0_daily_item_wind_direction = results_0_daily_item["wind_direction"]; // "东北", "西南", ...
    const char* results_0_daily_item_wind_direction_degree = results_0_daily_item["wind_direction_degree"];
    const char* results_0_daily_item_wind_speed = results_0_daily_item["wind_speed"]; // "23.4", "8.4", ...
    const char* results_0_daily_item_wind_scale = results_0_daily_item["wind_scale"]; // "4", "2", "1"
    const char* results_0_daily_item_humidity = results_0_daily_item["humidity"]; // "69", "77", "99"

  }

  const char* results_0_last_update = results_0["last_update"]; // "2022-02-17T08:00:00+08:00"

  Serial.println();
  Serial.println();
  Serial.println();
  // Wait 5 seconds
  delay(1000);
}
